package com.heyqing.ems.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heyqing.ems.common.MemberType;
import com.heyqing.ems.domain.BossInfo;
import com.heyqing.ems.domain.LoginManagement;
import com.heyqing.ems.domain.ManagerInfo;
import com.heyqing.ems.mapper.BossInfoMapper;
import com.heyqing.ems.mapper.LoginManagementMapper;
import com.heyqing.ems.po.ModifyPo;
import com.heyqing.ems.po.OpManagerPo;
import com.heyqing.ems.po.QueryPo;
import com.heyqing.ems.service.BossService;
import com.heyqing.ems.service.LoginService;
import com.heyqing.ems.service.ManagerService;
import com.heyqing.ems.util.IdUtil;
import com.heyqing.ems.vo.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName:BossServiceImpl
 * Package:com.heyqing.ems.service.impl
 * Description:
 *
 * @Date:2024/6/26
 * @Author:Heyqing
 */
@Service
@RequiredArgsConstructor
public class BossServiceImpl extends ServiceImpl<BossInfoMapper, BossInfo> implements BossService {

    private final ManagerService managerService;
    private final LoginManagementMapper loginManagementMapper;

    @Override
    public Result getList(QueryPo queryPo) {
        if (Objects.isNull(queryPo)){
            return Result.fail();
        }
        Page<BossInfo> resPage = lambdaQuery()
                .like(BossInfo::getName, queryPo.getQuery())
                .page(new Page<BossInfo>(queryPo.getPageNum(), queryPo.getPageSize()));
        if (Objects.isNull(resPage) || Objects.isNull(resPage.getRecords()) || Objects.isNull(resPage.getTotal())){
            return Result.fail();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list",resPage.getRecords());
        map.put("total",resPage.getTotal());
        return Result.success(map);
    }

    @Override
    @Transactional
    public Result saveManger(OpManagerPo opManagerPo) {
        if (Objects.isNull(opManagerPo)){
            return Result.fail();
        }
        Long id = opManagerPo.getId();
        //服务代理
        ManagerInfo managerInfo = managerService.getById(id);
        ManagerInfo manager = new ManagerInfo();
        BeanUtil.copyProperties(opManagerPo,manager);
        if (Objects.isNull(managerInfo)){
            // 添加
            managerService.save(manager);
        }else {
            // 修改
            managerService.updateById(manager);
        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result delete(Long id) {
        managerService.removeById(id);
        loginManagementMapper.deleteById(id);
        return Result.success();
    }

    @Override
    @Transactional
    public Result modify(ModifyPo modifyPo) {
        if (Objects.isNull(modifyPo)){
            return Result.fail();
        }
        Long id = modifyPo.getId();
        // 服务代理
        LoginManagement member = loginManagementMapper.selectById(id);
        LoginManagement loginManagement = new LoginManagement();
        BeanUtil.copyProperties(modifyPo,loginManagement);
        loginManagement.setType(MemberType.EMPLOYEE);
        if (Objects.isNull(member)){
            // 添加
            loginManagement.setId(IdUtil.getCurrentId());
            loginManagementMapper.insert(loginManagement);
        }else {
            // 修改
            loginManagementMapper.updateById(loginManagement);
        }
        return Result.success();
    }
}
